package com.hzcx.app.simplechat.base;

import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.event.FinishEvent;
import com.hzcx.app.simplechat.ui.publicui.PublicConfirmDialog;
import com.hzcx.app.simplechat.ui.publicui.PublicTipsDialog;
import com.hzcx.app.simplechat.util.statusbar.StatusBarUtil;
import com.hzcx.app.simplechat.util.toast.ToastUtils;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;

/**
 *   activity基类
 * @param <T>
 */
public abstract class BaseActivity<T extends BasePresenter> extends RxFragmentActivity implements IView {

    public static final String TAG=BaseActivity.class.getSimpleName();

    protected T mPresenter;

    private BaseLoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
        //去除title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if(Build.VERSION.SDK_INT!=Build.VERSION_CODES.O) {
            //竖屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        //开始不弹输入框
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        StatusBar();

        changeStatusBarTextColor(true);

        StatusBarUtil.setTransparent(this);


        setContentView(getLayout());
        ButterKnife.bind(this);

        initPresenter();
        initView();
        initData();

        if (mPresenter != null) {
            mPresenter.attachView(this);
        }

        if(findViewById(R.id.iv_back)!=null){
            findViewById(R.id.iv_back).setOnClickListener(view -> finish());
        }

        if(findViewById(R.id.tv_back)!=null){
            findViewById(R.id.tv_back).setOnClickListener(view -> finish());
        }
    }

    public abstract int getLayout();

    public abstract void initPresenter();

    public abstract void initView();

    public abstract void initData();

    /**
     *  设置状态栏颜色
     */
    protected void StatusBar() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.color_white), 100);
    }

    /**
     *  设置状态栏颜色深或浅
     * @param isBlack
     */
    protected void changeStatusBarTextColor(boolean isBlack) {
        View decor = getWindow().getDecorView();
        if (isBlack) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void showError(String msg) {
        ToastUtils.show(msg);
    }

    @Override
    public void showLoading() {
        try {
            loadingDialog=new BaseLoadingDialog(this);
            loadingDialog.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void hideLoading() {
        try {
            if(loadingDialog!=null&&loadingDialog.isShowing()){
                loadingDialog.dismiss();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void dataEmpty() {

    }

    /**
     *  通用的提示弹窗
     */
    @Override
    public void showTips(String title, String content, PublicTipsDialog.OnPublicTipsClickListener listener) {
        PublicTipsDialog tipsDialog=new PublicTipsDialog(this,title,content,listener);
        tipsDialog.show();
    }

    /**
     *  通用的确认提示框
     * @param title
     * @param content
     * @param confirm
     * @param listener
     */
    public void showConfirmDialog(String title, String content, String confirm, PublicConfirmDialog.OnPublicConfirmClickListener listener){
        PublicConfirmDialog confirmDialog=new PublicConfirmDialog(this,title,content,confirm,listener);
        confirmDialog.show();
    }

    /**
     *  退出所有界面
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void finishAll(FinishEvent event){
        finish();
    }
}
